/*HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
 H       cpu_sim.h
 H       Application name: tcss422-project2
 H       Programmers names:	Evan Phillips, Lucas Cowell, Tony Butterfield
 H       Date created:	May 13, 2011
 H       Date completed: 
 H       Brief Description: The CPU counts through the process steps and invokes the scheduler
 H                      when it recieves an interrupt.
 H
 HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH*/


#ifndef CPU_SIM_H_
#define CPU_SIM_H_
#endif /* CPU_SIM_H_ */

/* FORWARD DEFINITIONS - resolves circular dependencies */
struct process_queue_str;       // forward definition of a process queue
struct process_str;             // forward definition of a process.
struct io_device_str;
struct scheduler_str;

/***************************************************************************************************
 *  ADT: CPU
 *  Description:  The CPU is mostly a big counter that starts at the running process'
 *          "next step" and keeps incrementing until it runs out of steps and loops back to zero.
 *          Each step, it checks the process's request array for system calls and the interrupt
 *          controller for hardware interrupts and invokes the scheduler if any are found.
 *
 *
 *  Implementation:  Uses a struct to store data to be used by the sim,
 *
 ***************************************************************************************************/
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 $ Typedefs
 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
typedef struct cpu_str
{
        struct process_str* running_process;// pointer to the active process
        int program_counter;                // counter that loops through the active process' steps
        int interrupt_controller;           // holds interrupts to be processed.

        struct io_device_str* kbd;  // might need to change this depending on what we come up with
        struct io_device_str* dsk;
        struct io_device_str* vid;

        struct semaphore_str* sem;  // pointer to this system's semaphore

        struct scheduler_str* scheduler;

} CPUStr;
typedef CPUStr* CPU;


/*-------------------------------------------------------------------------------------------------
 - Prototypes
 -      Initialize  - create an new cpu.
 -      Add IO device - adds
 -      Terminate   - deallocates memory allocated in Initialize.
 -      Tick        - increments the PC (counting the process steps) and checks to see if the
 -                      process generates a Request, and/or there is an interrupt in the Interrupt
 -                      Controller
 -      Save State  - if there is an interrupt/request, the cpu updates the state of the running
 -                      process and calls the scheduler (separate command?) to change contexts
 -      Call Sched  - tells scheduler to take away the current process and serve up the next
 -                          ready process.
 ---------------------------------------------------------------------------------------------------*/
CPU     cpu_init();             // initializes the cpu.
int     cpu_free();             // we REALLY don't need to implement this.
int     cpu_tick(CPU);          // system clock ticks the cpu

int     cpu_callScheduler(CPU); //?
int     cpu_saveState(CPU);        // saves PC to the PCB and sets process state to P_INTERRUPTED
